class AddressesController < ApplicationController
  
  before_filter :authorize
  
  layout :determine_layout
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @party = Party.find(params[:id])
    @address_pages, @addresses = paginate :addresses, :per_page => 10, :conditions => ["party_id = ?", @party]
  end

  def show
    @address = Address.find(params[:id])
  end

  def new
    @party = Party.find(params[:id])
    @address = Address.new
  end

  def create
    @party = Party.find(params[:id])
    @address = Address.new(params[:address])
    #uri = last_request_uri
    if @party.addresses.push(@address)
      flash[:notice] = 'Address was successfully created.'
      redirect_to ({:controller => @party.class.name.pluralize, :action => 'show', :id => @party})
    else
      render :action => 'new'
    end
  end

  def edit
    @address = Address.find(params[:id])
  end

  def update
    @address = Address.find(params[:id])
    @party = @address.party
    #uri = last_request_uri
    if @address.update_attributes(params[:address])
      flash[:notice] = 'Address was successfully updated.'
      redirect_to ({ :controller => @party.class.name.pluralize, :action => 'show', :id => @party })
    else
      render :action => 'edit'
    end
  end

  def destroy
    Address.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
